package blasthouse.states;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import blasthouse.objects.CollisionWaypoint;
 
public class MainMenuState extends BasicGameState {
 
    int stateID = -1, bwX = -250, bwY = 100, hwX = 1250, hwY = 145, scaleTracker = 0, imageDelayInMS = 75;
    Image mainMenu = null;
    Image blastWord = null, blastImage = null, houseWord = null, houseImage = null, option1 = null, 
    	option1Pressed = null, option2 = null, option2Pressed = null;
    float blastScale = 0, houseScale = 0, timer = 0, lastTimer = 0;
    float [] scaleValues = {0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 0.9f, 0.8f, 0.7f};
    Audio sound = null, music = null;
    boolean played = false, hover1 = false, hover2 = false;
    
    MainMenuState( int stateID ) {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() { return 0; }
 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	System.out.println("Main Menu is = " + mainMenu);
    	mainMenu = new Image("blasthouse/images/mainMenu.jpg");    	
    	blastWord = new Image("blasthouse/images/blastWord.png");
    	blastImage = new Image("blasthouse/images/blastImage.png");
    	houseWord = new Image("blasthouse/images/houseWord2.png");
    	houseImage = new Image("blasthouse/images/houseImage.png");
    	option1 = new Image("blasthouse/images/option1.png");
    	option1Pressed = new Image("blasthouse/images/option1Pressed.png");
    	option2 = new Image("blasthouse/images/option2.png");
    	option2Pressed = new Image("blasthouse/images/option2Pressed.png");
    	
		try {
			sound = AudioLoader.getAudio("WAV",
			       new FileInputStream("src/blasthouse/sounds/bomb-02.wav"));
			music = AudioLoader.getAudio("OGG", 
					new FileInputStream("src/blasthouse/sounds/metalman_goes_clubbing.ogg"));
		} catch (FileNotFoundException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace(); }
   
    	blastImage.draw(50, 100, blastScale);
    	houseImage.draw(150, 100, houseScale);    	  
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    	mainMenu.draw(0, 0); 
    	blastImage.draw(225 - blastImage.getWidth()*blastScale/2, 150 - blastImage.getHeight()*blastScale/2, blastScale);
    	houseImage.draw(550 - houseImage.getWidth()*houseScale/2, 250 - houseImage.getHeight()*houseScale/2, houseScale); 
    	blastWord.draw(bwX, bwY);
    	houseWord.draw(hwX, hwY);    
    	
    	if( played ){
    		// TODO: display menu options
    		if( hover1 ){
    			option1Pressed.draw(250, 350);
    		}else{
    			option1.draw(250, 350);
    		}
    		if( hover2 ){
    			option2Pressed.draw(250, 400);
    		}else{
    			option2.draw(250, 400);
    		}
    	}
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	lastTimer = timer;
    	timer += delta;
    	
    	Input input = gc.getInput();
    	int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();

    	if( bwX < 100 ){ bwX += 5; }
    	if( hwX > 325 ){ hwX -= 5; }
    	
    	if( bwX == 100 && hwX == 325){
    		if(played == false){ 
    			playSounds();
    			played = true;
    		}
    		loopImages();
    	}    	
    	
    	// Input responses
    	if ( new Rectangle(250, 350, option1.getWidth(), option1.getHeight()).contains(mouseX, mouseY) ){
            hover1 = true;
            if( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && played ){
            	sound.stop();
            	music.stop();
        		sbg.enterState(BlastHouse.GAMEPLAYSTATE);
            }    		
        }else{ hover1 = false; }
    	if ( new Rectangle(250, 400, option2.getWidth(), option2.getHeight()).contains(mouseX, mouseY) ){
    		hover2 = true;
    		if( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && played ){
    			sound.stop();
            	music.stop();
            	sbg.enterState(BlastHouse.CONNECTIONSTATE);
    		}
    	}else{ hover2 = false; }

    } 
    
    public void playSounds(){
    	sound.playAsSoundEffect(1.0f, 1.0f, false);   
    	music.playAsMusic(1f, 1f, true);
    }
    
    public void loopImages(){    	
		if(scaleTracker > scaleValues.length - 1){ scaleTracker = scaleValues.length - 1; }    		
		
		if(timer > imageDelayInMS){
			houseScale = scaleValues[scaleTracker];
			blastScale = scaleValues[scaleTracker];
	
			scaleTracker++;
			timer = 0;
		}
	}
}
